﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using System.Collections.Generic;
using DataTransferObject.DiaryAndTrackingTask.SupportClassForUpdateTask;

namespace JSKE.DiaryAndTracking
{
    /// <summary>
    /// Summary description for LineCurvesChartType.
    /// </summary>
    public class LineCurvesChartType : System.Windows.Forms.UserControl
    {
        private System.Windows.Forms.DataVisualization.Charting.Chart chart1;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ComboBox comboBoxChartType;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.ComboBox comboBoxPointLabels;
        private System.Windows.Forms.CheckBox checkBoxShowMargin;
        private System.Windows.Forms.CheckBox checkBoxShow3D;
        private CheckBox chkHideSmaillTasks;
        private CheckBox chkBoxHideZero;

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public LineCurvesChartType()
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();
            checkBoxShow3D.Checked = true;
            checkBoxShow3D.Checked = false;
            // TODO: Add any initialization after the InitForm call

        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code
        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
            System.Windows.Forms.DataVisualization.Charting.Legend legend1 = new System.Windows.Forms.DataVisualization.Charting.Legend();
            System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series();
            System.Windows.Forms.DataVisualization.Charting.Series series2 = new System.Windows.Forms.DataVisualization.Charting.Series();
            this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart();
            this.panel1 = new System.Windows.Forms.Panel();
            this.chkBoxHideZero = new System.Windows.Forms.CheckBox();
            this.chkHideSmaillTasks = new System.Windows.Forms.CheckBox();
            this.checkBoxShow3D = new System.Windows.Forms.CheckBox();
            this.checkBoxShowMargin = new System.Windows.Forms.CheckBox();
            this.comboBoxPointLabels = new System.Windows.Forms.ComboBox();
            this.label2 = new System.Windows.Forms.Label();
            this.comboBoxChartType = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // chart1
            // 
            this.chart1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.chart1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(243)))), ((int)(((byte)(223)))), ((int)(((byte)(193)))));
            this.chart1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            this.chart1.BorderlineColor = System.Drawing.Color.FromArgb(((int)(((byte)(181)))), ((int)(((byte)(64)))), ((int)(((byte)(1)))));
            this.chart1.BorderlineDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            this.chart1.BorderlineWidth = 2;
            this.chart1.BorderSkin.SkinStyle = System.Windows.Forms.DataVisualization.Charting.BorderSkinStyle.Emboss;
            chartArea1.Area3DStyle.Inclination = 40;
            chartArea1.Area3DStyle.IsClustered = true;
            chartArea1.Area3DStyle.IsRightAngleAxes = false;
            chartArea1.Area3DStyle.LightStyle = System.Windows.Forms.DataVisualization.Charting.LightStyle.Realistic;
            chartArea1.Area3DStyle.Perspective = 9;
            chartArea1.Area3DStyle.Rotation = 25;
            chartArea1.Area3DStyle.WallWidth = 3;
            chartArea1.AxisX.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisX.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisX.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisY.LabelStyle.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            chartArea1.AxisY.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.AxisY.MajorGrid.LineColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.BackColor = System.Drawing.Color.OldLace;
            chartArea1.BackGradientStyle = System.Windows.Forms.DataVisualization.Charting.GradientStyle.TopBottom;
            chartArea1.BackSecondaryColor = System.Drawing.Color.White;
            chartArea1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));
            chartArea1.BorderDashStyle = System.Windows.Forms.DataVisualization.Charting.ChartDashStyle.Solid;
            chartArea1.Name = "Default";
            chartArea1.ShadowColor = System.Drawing.Color.Transparent;
            this.chart1.ChartAreas.Add(chartArea1);
            legend1.BackColor = System.Drawing.Color.Transparent;
            legend1.Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Top;
            legend1.Font = new System.Drawing.Font("Trebuchet MS", 8.25F, System.Drawing.FontStyle.Bold);
            legend1.IsTextAutoFit = false;
            legend1.Name = "Default";
            this.chart1.Legends.Add(legend1);
            this.chart1.Location = new System.Drawing.Point(3, 3);
            this.chart1.Name = "chart1";
            series1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
            series1.BorderWidth = 3;
            series1.ChartArea = "Default";
            series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            series1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(65)))), ((int)(((byte)(140)))), ((int)(((byte)(240)))));
            series1.Legend = "Default";
            series1.MarkerSize = 8;
            series1.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Circle;
            series1.Name = "Series1";
            series1.ShadowColor = System.Drawing.Color.Black;
            series1.ShadowOffset = 2;
            series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
            series1.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
            series2.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(26)))), ((int)(((byte)(59)))), ((int)(((byte)(105)))));
            series2.BorderWidth = 3;
            series2.ChartArea = "Default";
            series2.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            series2.Color = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(224)))), ((int)(((byte)(64)))), ((int)(((byte)(10)))));
            series2.Legend = "Default";
            series2.MarkerSize = 9;
            series2.MarkerStyle = System.Windows.Forms.DataVisualization.Charting.MarkerStyle.Diamond;
            series2.Name = "Series2";
            series2.ShadowColor = System.Drawing.Color.Black;
            series2.ShadowOffset = 2;
            series2.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
            series2.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Double;
            this.chart1.Series.Add(series1);
            this.chart1.Series.Add(series2);
            this.chart1.Size = new System.Drawing.Size(725, 395);
            this.chart1.TabIndex = 1;
            // 
            // panel1
            // 
            this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.panel1.Controls.Add(this.chkBoxHideZero);
            this.panel1.Controls.Add(this.chkHideSmaillTasks);
            this.panel1.Controls.Add(this.checkBoxShow3D);
            this.panel1.Controls.Add(this.checkBoxShowMargin);
            this.panel1.Controls.Add(this.comboBoxPointLabels);
            this.panel1.Controls.Add(this.label2);
            this.panel1.Controls.Add(this.comboBoxChartType);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Location = new System.Drawing.Point(3, 404);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(722, 66);
            this.panel1.TabIndex = 2;
            // 
            // chkBoxHideZero
            // 
            this.chkBoxHideZero.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.chkBoxHideZero.Location = new System.Drawing.Point(446, 7);
            this.chkBoxHideZero.Name = "chkBoxHideZero";
            this.chkBoxHideZero.Size = new System.Drawing.Size(104, 24);
            this.chkBoxHideZero.TabIndex = 7;
            this.chkBoxHideZero.Text = "Hide Total";
            this.chkBoxHideZero.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.chkBoxHideZero.CheckedChanged += new System.EventHandler(this.chkBoxHideZero_CheckedChanged);
            // 
            // chkHideSmaillTasks
            // 
            this.chkHideSmaillTasks.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.chkHideSmaillTasks.Location = new System.Drawing.Point(208, 8);
            this.chkHideSmaillTasks.Name = "chkHideSmaillTasks";
            this.chkHideSmaillTasks.Size = new System.Drawing.Size(168, 24);
            this.chkHideSmaillTasks.TabIndex = 6;
            this.chkHideSmaillTasks.Text = "Hide details";
            this.chkHideSmaillTasks.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.chkHideSmaillTasks.CheckedChanged += new System.EventHandler(this.checkBoxShowMarginY_CheckedChanged);
            // 
            // checkBoxShow3D
            // 
            this.checkBoxShow3D.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.checkBoxShow3D.Location = new System.Drawing.Point(382, 37);
            this.checkBoxShow3D.Name = "checkBoxShow3D";
            this.checkBoxShow3D.Size = new System.Drawing.Size(168, 24);
            this.checkBoxShow3D.TabIndex = 5;
            this.checkBoxShow3D.Text = "Display chart as 3&D:";
            this.checkBoxShow3D.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.checkBoxShow3D.CheckedChanged += new System.EventHandler(this.checkBoxShow3D_CheckedChanged);
            // 
            // checkBoxShowMargin
            // 
            this.checkBoxShowMargin.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.checkBoxShowMargin.Location = new System.Drawing.Point(208, 37);
            this.checkBoxShowMargin.Name = "checkBoxShowMargin";
            this.checkBoxShowMargin.Size = new System.Drawing.Size(168, 24);
            this.checkBoxShowMargin.TabIndex = 4;
            this.checkBoxShowMargin.Text = "Show X Axis &Margin:";
            this.checkBoxShowMargin.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.checkBoxShowMargin.CheckedChanged += new System.EventHandler(this.checkBoxShowMargin_CheckedChanged);
            // 
            // comboBoxPointLabels
            // 
            this.comboBoxPointLabels.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBoxPointLabels.Items.AddRange(new object[] {
            "None",
            "Auto",
            "TopLeft",
            "Top",
            "TopRight",
            "Right",
            "BottomRight",
            "Bottom",
            "BottomLeft",
            "Left",
            "Center"});
            this.comboBoxPointLabels.Location = new System.Drawing.Point(98, 37);
            this.comboBoxPointLabels.Name = "comboBoxPointLabels";
            this.comboBoxPointLabels.Size = new System.Drawing.Size(104, 22);
            this.comboBoxPointLabels.TabIndex = 3;
            this.comboBoxPointLabels.SelectedIndexChanged += new System.EventHandler(this.comboBoxChartType_SelectedIndexChanged);
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(3, 36);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(89, 23);
            this.label2.TabIndex = 2;
            this.label2.Text = "Point &Labels:";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // comboBoxChartType
            // 
            this.comboBoxChartType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBoxChartType.Items.AddRange(new object[] {
            "Line",
            "Spline",
            "StepLine",
            "FastLine",
            "FastPoint"});
            this.comboBoxChartType.Location = new System.Drawing.Point(98, 9);
            this.comboBoxChartType.Name = "comboBoxChartType";
            this.comboBoxChartType.Size = new System.Drawing.Size(104, 22);
            this.comboBoxChartType.TabIndex = 1;
            this.comboBoxChartType.SelectedIndexChanged += new System.EventHandler(this.comboBoxChartType_SelectedIndexChanged);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(3, 8);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(89, 23);
            this.label1.TabIndex = 0;
            this.label1.Text = "Chart &Type:";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // LineCurvesChartType
            // 
            this.BackColor = System.Drawing.Color.White;
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.chart1);
            this.Font = new System.Drawing.Font("Verdana", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Name = "LineCurvesChartType";
            this.Size = new System.Drawing.Size(728, 470);
            this.Load += new System.EventHandler(this.LineCurvesChartType_Load);
            ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit();
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);

        }
        #endregion

        private void UpdateChartSettings()
        {
            // Set series chart type
            for (int i = 0; i < chart1.Series.Count; i++)
            {
                chart1.Series[i].ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), comboBoxChartType.Text, true);
            }
            // Set point labels
            if (comboBoxPointLabels.Text != "None")
            {
                for (int i = 0; i < chart1.Series.Count; i++)
                {
                    chart1.Series[i].IsValueShownAsLabel = true;
                }
                if (comboBoxPointLabels.Text != "Auto")
                {
                    for (int i = 0; i < chart1.Series.Count; i++)
                    {
                        chart1.Series[i]["LabelStyle"] = comboBoxPointLabels.Text;
                    }
                }
            }
            else
            {
                for (int i = 0; i < chart1.Series.Count; i++)
                {
                    chart1.Series[i].IsValueShownAsLabel = false;
                }
            }

            // Set X, Y axis margin
            chart1.ChartAreas["Default"].AxisX.IsMarginVisible = checkBoxShowMargin.Checked;
            chart1.ChartAreas["Default"].AxisX.Title = "Time";
            chart1.ChartAreas["Default"].AxisX.TitleAlignment = StringAlignment.Far;
            chart1.ChartAreas["Default"].AxisY.IsMarginVisible = chkHideSmaillTasks.Checked;
            chart1.ChartAreas["Default"].AxisY.Title = "Work remaining (min)";
            chart1.ChartAreas["Default"].AxisY.TitleAlignment = StringAlignment.Far;
        }

        private void LineCurvesChartType_Load(object sender, System.EventArgs e)
        {
            comboBoxChartType.SelectedIndex = 0;
            comboBoxPointLabels.SelectedIndex = 0;
            checkBoxShow3D.Checked = false;

            // Populate series data
            Random random = new Random();
            for (int i = 0; i < chart1.Series.Count; i++)
            {
                for (int pointIndex = 0; pointIndex < 10; pointIndex++)
                {
                    chart1.Series[i].Points.AddXY((i + 1) * (pointIndex + 1), random.Next((i + 1) * 150, (i + 3) * 200));
                }
            }

            UpdateChartSettings();
        }

        private void comboBoxChartType_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            UpdateChartSettings();
        }

        private void checkBoxShowMargin_CheckedChanged(object sender, System.EventArgs e)
        {
            UpdateChartSettings();
        }

        private void checkBoxShow3D_CheckedChanged(object sender, System.EventArgs e)
        {
            chart1.ChartAreas[0].Area3DStyle.Enable3D = checkBoxShow3D.Checked;
            if (checkBoxShow3D.Checked)
            {
                for (int i = 0; i < chart1.Series.Count; i++)
                {
                    chart1.Series[i].MarkerStyle = MarkerStyle.None;
                    chart1.Series[i].BorderWidth = 1;
                }
                chart1.Series[0].MarkerStyle = MarkerStyle.Star5;
                chart1.Series[0].BorderWidth = 2;
            }
            else
            {
                for (int i = 0; i < chart1.Series.Count; i++)
                {
                    chart1.Series[i].MarkerStyle = MarkerStyle.Circle;
                    chart1.Series[i].BorderWidth = 2;
                }
                chart1.Series[0].MarkerStyle = MarkerStyle.Star5;
                chart1.Series[0].BorderWidth = 4;
            }
        }
        private List<JskeSerie> seriesData = new List<JskeSerie>();
        Boolean isNewData = true;
        public void binData2Chart(List<JskeSerie> data, Boolean isAreaZero)
        {
            isNewData = true;
            addDataToChart(data, isAreaZero);
            adjustData();
            for (int i = 0; i < chart1.Series.Count; i++)
            {
                chart1.Series[i].MarkerStyle = MarkerStyle.Circle;
                chart1.Series[i].BorderWidth = 2;
            }
            chart1.Series[0].MarkerStyle = MarkerStyle.Star6;
            chart1.Series[0].BorderWidth = 4;
        }
        private void addDataToChart(List<JskeSerie> allSeries, Boolean isArea0)
        {
            if (allSeries.Count > 0)
            {
                if (isNewData)
                {
                    seriesData = allSeries;
                    isNewData = false;
                }
                chart1.Series.Clear();
                for (int i = 0; i < allSeries.Count; i++)
                {
                    chart1.Series.Add(new Series(allSeries[i].SerieName));
                    for (int j = 0; j < allSeries[i].allPoints.Count; j++)
                    {
                        chart1.Series[i].Points.AddXY(allSeries[i].allPoints[j].xVal, allSeries[i].allPoints[j].yVal);
                    }
                }
                if (isArea0)
                {
                    chart1.Series[0].ChartType = SeriesChartType.SplineArea;
                }
                UpdateChartSettings();
                

            }
        }

        private void checkBoxShowMarginY_CheckedChanged(object sender, EventArgs e)
        {
            adjustData();
            //if (chkHideSmaillTasks.Checked)
            //{
            //    if (chart1.Series.Count > 1)
            //    {
            //        chart1.Series.Clear();
            //        chart1.Series.Add(new Series(seriesData[0].SerieName));
            //        for (int j = 0; j < seriesData[0].allPoints.Count; j++)
            //        {
            //            chart1.Series[0].Points.AddXY(seriesData[0].allPoints[j].xVal, seriesData[0].allPoints[j].yVal);
            //        }
            //        chart1.Series[0].BorderWidth = 4;
            //        UpdateChartSettings();
            //    }
            //}
            //else
            //{
            //    addDataToChart(seriesData, true);
            //}
        }
        private void chkBoxHideZero_CheckedChanged(object sender, EventArgs e)
        {
            adjustData();
            //if (chkBoxHideZero.Checked)
            //{
            //    if (chart1.Series.Count > 1)
            //    {
            //        chart1.Series.RemoveAt(0);
            //        UpdateChartSettings();
            //    }
            //}
            //else
            //{
            //    for (int i = 0; i < chart1.Series.Count; i++)
            //    {
            //        addDataToChart(seriesData, true);
            //    }
            //}
        }

        private void adjustData()
        {
            if (chkBoxHideZero.Checked)
            {
                if (chkHideSmaillTasks.Checked)
                {
                    chart1.Series.Clear();
                }
                else
                {//chỉ show detail
                    addDataToChart(seriesData.GetRange(1, seriesData.Count - 1), true);
                    chart1.Series[0].BorderWidth = 1;
                }
            }
            else
            {//show zero
                if (chkHideSmaillTasks.Checked)
                {//không show detail
                    addDataToChart(seriesData.GetRange(0,1), true);
                    chart1.Series[0].BorderWidth = 4;
                }
                else
                {
                    addDataToChart(seriesData, true);
                    chart1.Series[0].BorderWidth = 4;
                }
            }
        }
    }
}
